#!/bin/bash

###################################################################
# Written By:
#             CSCoder4ever
#
#   use as server-backup
#
###################################################################

###################################################################
# Destination(s)
###################################################################
BACKUPDIR=/mnt/backup_mnt/`hostname`
BACKUPMNT=/mnt/backup_mnt

###################################################################
# Sources
###################################################################
HDD1=/mnt/hdd1
HDD2=/mnt/hdd2
HDD3=/mnt/hdd3

###################################################################
# shortcuts
###################################################################
USER=`id -un 1000`

###################################################################
# rsync flags
###################################################################
RSF="-av --delete"

###################################################################
# misc
###################################################################
EDITOR=vim
LASTVAL=0

###################################################################
# Functions
###################################################################
usage()
{
    echo "usage"
    echo -e "For backing up:
    '$(basename "$0") backup <hdd mount dir in /mnt> <device name>'

    acceptable <hdd mount dir in /mnt> values:
    hdd1, hdd2, hdd3

    acceptable <device name> values:
    sdX(number)

example:
    '$(basename "$0") backup hdd1 sdd1

For syncing (copying files from backup drive, like restoring.):

    '$(basename "$0") sync <hdd mount dir in /mnt> <device name>'
    
    acceptable <hdd mount dir in /mnt> values:
    hdd1, hdd2, hdd3

    acceptable <device name> values:
    sdX(number)

example:
    '$(basename "$0") backup hdd1 sdd1

Current rsync flags: 
to NAS: $RSF

To change source and destinations:

    '$(basename "$0") edit'

For help:

    '$(basename "$0") help'

"
    exit 1
}

handle_hdd_mount()
{
    mount $1 $BACKUPMNT
    LASTVAL=$? 

    if [ $LASTVAL -ne 0 ]; then
        echo "Error mounting backup drive, exiting..."
        exit 1
    fi

    if [ ! -f $BACKUPMNT/OKAY.txt ]; then
        echo -e "This hard drive isn't okay to use for backup,
please choose a different one (or add OKAY.txt to the root of the
backup hdd.)\n"
        umount $BACKUPMNT
        LASTVAL=$?

        if [ $LASTVAL -ne 0 ]; then
            echo -e "Error unmounting backup drive, try:
    'umount $BACKUPMNT'
manually, exiting...\n
"
            exit 1
        fi

        exit 1

    fi
}

handle_hdd_umount()
{
    umount $BACKUPMNT
    LASTVAL=$?

    if [ $LASTVAL -ne 0 ]; then
        echo -e "Error unmounting backup drive, try:
    'umount $BACKUPMNT'
manually, exiting...\n
"
        exit 1
    fi
}

sync_to()
{
    rsync $RSF /mnt/$1 $BACKUPDIR
}

sync_from()
{
    rsync $RSF $BACKUPDIR/$1 /mnt
}

check_args()
{
     if ! [[ "$1" =~ ^hdd[1-3]$ ]]; then
	    echo "Unacceptable value $1"
        echo "Try hdd1, hdd2, or hdd3"
        exit 1
     fi

        
     if ! [[ "$2" =~ ^sd[a-z][0-9]$ ]]; then
        echo "Unacceptable value $2"
        echo "Try sd[a-z][0-9]"
        exit 1
     fi
}

# Check if user is root
if [ "$(id -u)" != "0" ]; then

    # help is the only command one can run without root.

    if ! [[ "$1" =~ ^[Hh]|[Hh][Ee][Ll][Pp]|-[Hh]|--[Hh][Ee][Ll][Pp]$ ]]; then
    	echo "$(basename "$0") needs to be run as root."
    	usage
    fi
fi

case $1 in
    -[Hh]|--[Hh][Ee][Ll][Pp])
        echo -e "$(basename "$0") is a simple script
        which uses rsync to copy data to or from a storage
        machine, like a NAS of sorts.
        
        "

        usage 
    ;;
    
    [Bb][Aa][Cc][Kk][Uu][Pp])
        check_args $2 $3
        handle_hdd_mount /dev/$3
        sync_to $2
        handle_hdd_umount
    ;;

    [Ss][Yy][Nn][Cc])
        check_args $2 $3 
        handle_hdd_mount /dev/$3
        sync_from $2
        handle_hdd_umount
    ;;

    [Ee]|[Ee][Dd][Ii][Tt]|-[Ee]|--[Ee][Dd][Ii][Tt])
        $EDITOR /usr/bin/$(basename "$0")
    ;;

    *)
        usage
    ;;
esac

exit 0
